package bg.uni_sofia.conf_manager.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import bg.uni_sofia.conf_manager.model.Conference;
import bg.uni_sofia.conf_manager.model.Employee;
import bg.uni_sofia.conf_manager.model.Lecture;

@Stateless
@Path("/emessages")
public class EmployeeMassages {

	
	@PersistenceContext
	private EntityManager em;
	
	@Inject LoginBean sessionData;
	
	
	public EmployeeMassages(){
		
	}
	
	@GET
	@Path("/newLectures")
	@Produces("application/json")
	public List<Lecture> getEmployeeNewLectures() {
		TypedQuery<Conference> confQuery = em.createNamedQuery("getConferencesByEmployee", Conference.class);
    	confQuery.setParameter("id", sessionData.getId());
    	List<Conference> myConferences = confQuery.getResultList();
    	
    	TypedQuery<Employee> empQuery = em.createNamedQuery("getEmployeeById", Employee.class);
    	empQuery.setParameter("id", sessionData.getId());
    	List<Employee> employeesList = empQuery.getResultList();

    	List<Lecture> lectures = new ArrayList<Lecture>();
    	
    	if (myConferences.size()!=0) {
			
    		for (Conference conf: myConferences) {
				
    			List<Lecture> confLectures = conf.getLectures();
    			
    			if(confLectures.size()!=0){
    				
    				for (Lecture lec: confLectures) {
						
    					if (employeesList.get(0).getLastLogin().compareTo(lec.getCreateDate())<0) {
							lectures.add(lec);
						}
						
					}
    				
    				
    			}
				
			}
		}
    	return lectures;
	}
	
	@GET
	@Path("/notSeen")
	@Produces("application/json")
	public List<Lecture> getNotSeenLectures() {
//		System.out.println("Second:");
		TypedQuery<Conference> confQuery = em.createNamedQuery("getConferencesByEmployee", Conference.class);
    	confQuery.setParameter("id", sessionData.getId());
    	List<Conference> myConferences = confQuery.getResultList();
    	
    	TypedQuery<Employee> empQuery = em.createNamedQuery("getEmployeeById", Employee.class);
    	empQuery.setParameter("id", sessionData.getId());
    	List<Employee> employeesList = empQuery.getResultList();
    	List<Lecture> lectures = new ArrayList<Lecture>();
    	
    	if (myConferences.size()!=0) {
			
    		for (Conference conf: myConferences) {
    			
    			Date now = new Date();
    			long timeDiff = conf.getStartTime().getTime() - now.getTime();
    			long timeDiffDays = timeDiff/(1000 * 60 * 60 * 24);
    			if (timeDiffDays <= 20) {
    				
    				List<Lecture> confLectures = conf.getLectures();
        			
        			if(confLectures.size()!=0){
        				
        				for (Lecture lec: confLectures) {
    						
        					if (lec.getStatus() == 0) {
    							lectures.add(lec);
    						}		
    					}        				
        			}
    			}
				
			}
		}
    	return lectures;
	}
	
	
	
}
